package org.act.BPEL2Automata.automata.module;

import java.util.LinkedList;

/*
 * TODO This Message class should be updated,as some properties 
 * do NOT contain enough informations.
 */

/**
 * 
 * definition of Message.
 * It consists of PartnerLink,PortType,Operation,MessageType and Direction.
 * @author baoxiwu<bb@feijiao.info>,ACT,Beihang University
 * @version 0.9.0
 * @since 2010-03-23
 * 
 */
public class Message {
	private static long count = 0l;
	public final static String MESSAGE_DIRECTION_IN = "in";
	public final static String MESSAGE_DIRECTION_OUT = "out";
	
	//TODO maybe it's NOT necessary.
	private String messageID = "";
	
	private PartnerLink partnerLink = new PartnerLink();
	private PortType portType = new PortType();
	private Operation operation = new Operation();
	private MessageType messageType = new MessageType();
	private String direction = Message.MESSAGE_DIRECTION_IN;
	
	public Message(){
		if(Message.count < Long.MAX_VALUE){
			Message.count++;
			this.messageID = Long.toString(Message.count);
		}
		else{
		//TODO
		//common EXCEPTION_HANDLER WANTED.
		}
	}
	
	public Message(String partnerLink,String portType,String operation,String messageType,String direction){
		if(Message.count < Long.MAX_VALUE){
			Message.count++;
			this.messageID = Long.toString(Message.count);
			
			this.partnerLink = new PartnerLink(partnerLink);
			this.portType = new PortType(portType);
			this.operation = new Operation(operation);
			this.messageType = new MessageType(messageType);
			this.direction = direction;
		}
		else{
		//TODO
		//common EXCEPTION_HANDLER WANTED.
		}
	}
	
	public String getID(){
		return this.messageID;
	}
	
	//getter and setter methods
	public PartnerLink getPartnerLink(){
		return this.partnerLink;
	}
	
	public void setPartnerLink(PartnerLink partnerLink){
		this.partnerLink = partnerLink;
	}
	
	public PortType getPortType(){
		return this.portType;
	}
	
	public void setPortType(PortType portType){
		this.portType = portType;
	}
	
	public Operation getOperation(){
		return this.operation;
	}
	
	public void setOperation(Operation operation){
		this.operation = operation;
	}
	
	public MessageType getMessageType(){
		return this.messageType;
	}
	
	public void setMessageType(MessageType messageType){
		this.messageType = messageType;
	}
	
	public String getDirection(){
		return this.direction;
	}
	
	public void setDirection(String direction){
		this.direction = direction;
	}
	
	public String toString(){
		String result = ("\tmes" + this.getID() 
				+ "\n\t\tpartnerLink : " + this.getPartnerLink().getName()
				+ "\n\t\tportType : " + this.getPortType().getName()
				+ "\n\t\toperation : " + this.getOperation().getName()
				+ "\n\t\tmessageType : " + this.getMessageType().getName()
				+ "\n\t\tdirection : " + this.getDirection() + "\n");
		
		return result;
	}
	
	/**
	 * check <b>message</b>'s MessageType,PortType and Operation to decide whether it exists 
	 * as a member of <b>messageSet</b>,if true replace <b>message</b> 
	 * by the element in <b>messageSet</b>.
	 * @param messageSet
	 * @param message
	 */
	public static Message checkUnicity(LinkedList<Message> messageSet,Message message){
		for(int i = 0; i < messageSet.size(); i++){
			if(messageSet.get(i).getMessageType().getName().equals(message.getMessageType().getName())
					&& messageSet.get(i).getPortType().getName().equals(message.getPortType().getName())
					&& messageSet.get(i).getOperation().getName().equals(message.getOperation().getName())){
				message = messageSet.get(i);
				
				return message;
			}
		}
		
		return message;
	}
}
